package com.example.saiaadmin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.saiaadmin.entry.EbSystemDictData;
import com.example.saiaadmin.service.EbSystemDictDataService;
import com.example.saiaadmin.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典数据表Controller
 *
 *  @author  Vagrant
 * @since 2025/01/13
 */
@RestController
@RequestMapping("/eb-system-dict-data")
public class EbSystemDictDataController {

    @Autowired
    private EbSystemDictDataService service;

    @GetMapping("/dictAll")
    public ResultData<Map> dictAll(){
        LambdaQueryWrapper<EbSystemDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(EbSystemDictData::getSort);
        List<EbSystemDictData> list = service.list(queryWrapper);
        //List<String> codeList = list.stream().map(item->item.getCode()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//        List<List<EbSystemDictData>> groupedByAge = list.stream()
//                .collect(Collectors.groupingBy(EbSystemDictData::getCode))
//                .values()
//                .stream()
//                .collect(Collectors.toList());
        Map<String, List<EbSystemDictData>> listMap = list.stream()
                .collect(Collectors.groupingBy(EbSystemDictData::getCode));
       return ResultData.success("查询成功",listMap);
    }

    /**
     * 快捷查询字典
     * @param code
     * @return
     */
    @GetMapping("/dictData")
    public ResultData<List> dictData(String code){
        LambdaQueryWrapper<EbSystemDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null !=code,EbSystemDictData::getCode,code);
        queryWrapper.orderByAsc(EbSystemDictData::getSort);
        List<EbSystemDictData> list = service.list(queryWrapper);
        return ResultData.success("查询成功",list);
    }

}
